package org.nimi317.web_gis.controller;

import cn.dev33.satoken.annotation.SaCheckLogin;
import org.nimi317.web_gis.Enum.ProvinceDataEnum;
import org.nimi317.web_gis.entity.ProvinceQuality;
import org.nimi317.web_gis.entity.R;
import org.nimi317.web_gis.form.post.ProvinceRenderPost;
import org.nimi317.web_gis.model.AirQualityPredict;
import org.nimi317.web_gis.service.ProvinceService;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.Comparator;
import java.util.List;

/**
 * @author thunderobot
 */
@RestController
@RequestMapping("/provinces")
@SaCheckLogin
public class ProvinceController {
    private final ProvinceService provinceService;

    private final AirQualityPredict predict;

    public ProvinceController(ProvinceService provinceService, AirQualityPredict predict) {
        this.provinceService = provinceService;
        this.predict = predict;
    }

    @GetMapping
    public R<?> getProvinces() {
        return R.ok(provinceService.list());
    }

    @PostMapping("history")
    public R<?> getHistory(@RequestBody @Validated ProvinceRenderPost post) {
        return R.ok(provinceService.getHistory(post));
    }

    /**
     * 获取当前省份优质天数以及 污染天数中主要污染源
     *
     * @return R
     */
    @GetMapping("health/{id}")
    @Deprecated
    public R<?> getHealth(@PathVariable Integer id) {
        //当前月份 一个是首要污染物浓度天数 一个是优质天数和污染物天数
        List<ProvinceQuality> history = provinceService.getHistory(new ProvinceRenderPost(id, ProvinceDataEnum.Month));
        return R.ok(history);
    }

    @GetMapping("predict/{id}")
    public R<?> getPredict(@PathVariable Integer id) {
        List<ProvinceQuality> recurrent = provinceService.getRecurrent(id);
        List<AirQualityPredict.Data> predictData = predict.predictData(recurrent.stream().sorted(Comparator.comparing(ProvinceQuality::getTimestamp)).toList());
        return R.ok(predictData);
    }

    @GetMapping("realTime")
    public R<?> getRealTime() {
        return R.ok(provinceService.getRealTime());
    }
}
